<?php
require_once "BaseSQLDAO.php";
require_once dirname(__FILE__)."/../SMSDAO.php";
require_once "SQLStatements.php";
require_once "ErrorMessages.php";
require_once dirname(__FILE__)."/../DataAccessException.php";
require_once dirname(__FILE__)."/../../model/SMS.php";


class SMSSQLDAOImpl extends BaseSQLDAO implements SMSDAO {

    public function create($object) {
        $stmt = $this->getConnection()->prepare(SQLStatements::SMS_INSERT);
        $stmt->bind_param("iisssdds", $object->getId(), $object->getSenderId(), $object->getRecipientMobileNumber(), $object->getRecipientName(), $object->getMessage(), $object->getLastModifiedDate(), $object->getSentDate(), $object->getStatus());
        $stmt->execute();
        if ($stmt->affected_rows != 1) {
            throw new DataAccessException(ErrorMessages::OBJECT_CREATE_ERROR);
        }
    }

    public function read($id) {
        $sms = new SMS();
        $stmt = $this->getConnection()->prepare(SQLStatements::SMS_SELECT_BY_ID);
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $stmt->bind_result($smsId, $senderId, $recipientMobileNumber, $recipientName, $message, $lastModifiedDate, $sentDate, $status);
        $stmt->store_result();
        $stmt->fetch();
        if ($stmt->num_rows != 1) {
            throw new DataAccessException(ErrorMessages::OBJECT_READ_ERROR);
        }

        $sms->setId($smsId);
        $sms->setSenderId($senderId);
        $sms->setRecipientMobileNumber($recipientMobileNumber);
        $sms->setRecipientName($recipientName);
        $sms->setMessage($message);
        $sms->setLastModifiedDate($lastModifiedDate);
        $sms->setSentDate($sentDate);
        $sms->setStatus($status);

        return $sms;
    }

    public function getSMSByUserId($id) {
        $smsArray = array();

        $stmt = $this->getConnection()->prepare(SQLStatements::SMS_SELECT_MANY_BY_SENDER_ID);
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $stmt->bind_result($smsId, $senderId, $recipientMobileNumber, $recipientName, $message, $lastModifiedDate, $sentDate, $status);
        $stmt->store_result();
        while ($stmt->fetch()) {
            $sms = new SMS();
            $sms->setId($smsId);
            $sms->setSenderId($senderId);
            $sms->setRecipientMobileNumber($recipientMobileNumber);
            $sms->setRecipientName($recipientName);
            $sms->setMessage($message);
            $sms->setLastModifiedDate($lastModifiedDate);
            $sms->setSentDate($sentDate);
            $sms->setStatus($status);
            $smsArray[] = $sms;
        }

        return $smsArray;
    }

    public function getSMSByStatus($status, $userId) {
        $smsArray = array();

        $stmt = $this->getConnection()->prepare(SQLStatements::SMS_SELECT_MANY_BY_STATUS);
        $stmt->bind_param("si", $status, $userId);
        $stmt->execute();
        $stmt->bind_result($smsId, $senderId, $recipientMobileNumber, $recipientName, $message, $lastModifiedDate, $sentDate, $status);
        $stmt->store_result();
        while ($stmt->fetch()) {
            $sms = new SMS();
            $sms->setId($smsId);
            $sms->setSenderId($senderId);
            $sms->setRecipientMobileNumber($recipientMobileNumber);
            $sms->setRecipientName($recipientName);
            $sms->setMessage($message);
            $sms->setLastModifiedDate($lastModifiedDate);
            $sms->setSentDate($sentDate);
            $sms->setStatus($status);
            $smsArray[] = $sms;
        }

        return $smsArray;
    }

    public function update($object) {


        $stmt = $this->getConnection()->prepare(SQLStatements::SMS_UPDATE);
        $stmt->bind_param("isssddsi", $object->getSenderId(), $object->getRecipientMobileNumber(), $object->getRecipientName(), $object->getMessage(), $object->getLastModifiedDate(), $object->getSentDate(), $object->getStatus(), $object->getId());
        $stmt->execute();
        if ($stmt->affected_rows != 1) {
            throw new DataAccessException(ErrorMessages::OBJECT_UPDATE_ERROR);
        }
    }

    public function delete($id) {
        $stmt = $this->getConnection()->prepare(SQLStatements::SMS_DELETE_BY_ID);
        $stmt->bind_param("i", $id);
        $stmt->execute();
        if ($stmt->affected_rows != 1) {
            throw new DataAccessException(ErrorMessages::OBJECT_DELETE_ERROR);
        }
    }

    public function deleteAll() {
        $stmt = $this->getConnection()->prepare("DELETE FROM sms");
        $stmt->execute();
    }

    public function count() {
        $stmt = $this->getConnection()->prepare("SELECT COUNT(sms_id) FROM sms");
        $stmt->execute();
        $stmt->bind_result($count);
        $stmt->fetch();
        return $count;
    }

}
?>
